﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CustomControls_Procesos : System.Web.UI.UserControl
{
    protected int Id
    {
        get
        {
            if (ViewState["id"] == null) return 0;
            return (int)ViewState["id"];
        }
        set { ViewState["id"] = value; }
    }
    protected String[] Message
    {
        get
        {
            if (Session["Message"] == null)
            {
                String[] m = {"",""};
                return m;
            }
            return (String[])Session["Message"];
        }
        set { Session["Message"] = value; }
    }
    private bool Add
    {
        get
        {
            if (ViewState["add"] != null)
            {
                return (bool)ViewState["add"];
            }
            else
            {
                return false;
            }
        }
        set
        {
            ViewState["add"] = value;
        }
    }
    private bool Edit
    {
        get
        {
            if (ViewState["Edit"] != null)
            {
                return (bool)ViewState["Edit"];
            }
            else
            {
                return false;
            }
        }
        set
        {
            ViewState["Edit"] = value;
        }
    }
    private bool View
    {
        get
        {
            if (ViewState["View"] != null)
            {
                return (bool)ViewState["View"];
            }
            else
            {
                return false;
            }
        }
        set
        {
            ViewState["View"] = value;
        }
    }
    private bool Disable
    {
        get
        {
            if (ViewState["Disable"] != null)
            {
                return (bool)ViewState["Disable"];
            }
            else
            {
                return false;
            }
        }
        set
        {
            ViewState["Disable"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            String[] msg = { "", "" };
            Message = msg;
            divSample.Visible = false;
            if (!String.IsNullOrEmpty(Request.QueryString["id"]) && Request.Url.ToString().Contains("Editar.aspx"))
            {
                int cid = 0;
                int.TryParse(Request.QueryString["id"], out cid);
                this.Id = cid;
                BLLProcess bll = new BLLProcess();
                Process p = bll.getProcessById(this.Id);
                if (p.Id > 0)
                {
                    populateInfo(p);
                }
                else
                {
                    String[] error = { Resources.Resource.ERROR, Resources.Resource.PROCESS_NOT_FOUND};
                    Message = error;
                    this.Response.Redirect("Default.aspx?recover=y");
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    String[] error = { Resources.Resource.ERROR, Resources.Resource.INVALID_URL};
                    Message = error;
                    this.Response.Redirect("Default.aspx?recover=y");
                }
            }
            SetupPageByUserRoles();
        }
        ((Literal)Page.Master.FindControl("ltrShowMessage")).Text = "0";
    }
    private void populateInfo(Process p)
    {
        txtName.Text = p.Name;
        txtDescription.Text = p.Description;
        chkEnabled.Checked = p.IsEnabled;
        hdnSample.Value = p.SampleImage;
        divSample.Visible = true;
        imgSample.ImageUrl = "~/Procesos/Muestras/" + p.SampleImage;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Process p = new Process();
        p.Name = txtName.Text;
        p.Description = txtDescription.Text;
        p.IsEnabled = chkEnabled.Checked;
        if (this.Id == 0)
        {
            p.CreatedBy = Util.GetCurrentUserId();
            p.CreatedDate = DateTime.Now;
        }
        else
        {
            p.Id = this.Id;
        }
        if (uplSample.HasFile)
        {
            FileInfo fi = new FileInfo(uplSample.FileName);
            String filename = System.Guid.NewGuid().ToString()+fi.Extension;
            try
            {
                uplSample.SaveAs(Server.MapPath("~/Procesos/Muestras/") + filename);
                p.SampleImage = filename;
            }
            catch
            {
                ((Literal)Page.Master.FindControl("ltrShowMessage")).Text = "1";
                ((Literal)Page.Master.FindControl("ltrMessage")).Text = Resources.Resource.UPDATE_ERROR;
                ((Literal)Page.Master.FindControl("ltrMessageType")).Text = Resources.Resource.ERROR;
            }
        }
        else {
            p.SampleImage = hdnSample.Value;
        }
        BLLProcess bll = new BLLProcess();
        if (bll.Save(p))
        {
            String[] error = { Resources.Resource.SUCCESS, Resources.Resource.PROCESS_CREATED };
            Message = error;
            this.Response.Redirect("Default.aspx?recover=y");
        }
        else
        {
            ((Literal)Page.Master.FindControl("ltrShowMessage")).Text = "1";
            ((Literal)Page.Master.FindControl("ltrMessage")).Text = Resources.Resource.UPDATE_ERROR;
            ((Literal)Page.Master.FindControl("ltrMessageType")).Text = Resources.Resource.ERROR;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.Response.Redirect("Default.aspx?recover=y");
    }
    protected void SetupPageByUserRoles()
    {
        List<SessionPermissions> userPermissions = new List<SessionPermissions>();
        userPermissions = (List<SessionPermissions>)Session[GlobalNames.UserPermissions];
        userPermissions = (from upb in userPermissions
                           where upb.ModuleId == (long)GlobalEnums.Modules.Procesos
                           select upb).ToList();
        if (userPermissions.Count() > 0)
        {
            foreach (SessionPermissions up in userPermissions)
            {
                if (up.PermissionId == (long)GlobalEnums.Permissions.Agregar)
                    Add = true;
                if (up.PermissionId == (long)GlobalEnums.Permissions.Editar)
                    Edit = true;
                if (up.PermissionId == (long)GlobalEnums.Permissions.Ver)
                    this.View = true;
                if (up.PermissionId == (long)GlobalEnums.Permissions.Habilitar_Deshabilitar)
                    this.Disable = true;
            }
            if (!View)
                this.Response.Redirect("~/Default.aspx");
            if (!Add && Id == 0)
            {
                this.Response.Redirect("Default.aspx");
            }
            if (Id > 0 && !Edit)
                btnSave.Visible = false;
            if (!Disable)
                chkEnabled.Enabled = false;
        }
        else
        {
            this.Response.Redirect("~/Default.aspx");
        }
    }
}